package hz.mobileproject.pda.ui.base;

import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.zxing.activity.CaptureActivity;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.broadcast.HandleBroadcast;
import hz.mobileproject.pda.broadcast.IShowBarcodeStr;
import hz.mobileproject.pda.toolsmanager.bean.response.RP_ToolSysSet;
import hz.mobileproject.pda.ui.view.LoadingDialog;
import hz.mobileproject.pda.utils.CommonUtil;

/**
 * Created by Lc on 2017/12/18.
 */
public abstract class BaseScanActivity extends AppCompatActivity implements IShowBarcodeStr {
    @ViewInject(R.id.tv_title_assets)
    private TextView tv_title_assets;
    @ViewInject(R.id.iv_title_right)
    private ImageView iv_title_right;
    /**
     * 扫码相关
     */
    protected LoadingDialog mScanLoadingDialog;
    protected HandleBroadcast mHandleBroadcast;
    protected SoundPool mSoundPool;
    protected int mSoundId;
    //上次扫码时间
    private long mLastScanTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mScanLoadingDialog = new LoadingDialog(this);
        initData();
    }


    /**
     * 打开扫描
     * @param requestCode 请求码
     * @param scanType 配置信息 RP_ToolSysSet.TSS_SWEEP_TYPE_CAMERA = 2，RP_ToolSysSet.TSS_SWEEP_TYPE_INFRARED = 1
     * @param dialogMsg 弹窗提示消息
     */
    protected void openScan(int requestCode , RP_ToolSysSet scanType, String dialogMsg) {
        if (RP_ToolSysSet.TSS_SWEEP_TYPE_CAMERA == scanType.getSweep_type()) {  //打开相机扫描
            //打开二维码扫描界面
            if (CommonUtil.isCameraCanUse()) {
                Intent intent = new Intent(this, CaptureActivity.class);
                intent.putExtra("title",dialogMsg);
                startActivityForResult(intent, requestCode);
            }else {
                ToastUtils.showLong("请打开此应用的摄像头权限！");
            }
        } else if(RP_ToolSysSet.TSS_SWEEP_TYPE_INFRARED == scanType.getSweep_type()){
            if(mScanLoadingDialog.isShowing()){
                mScanLoadingDialog.dismiss();
            }
            mScanLoadingDialog.setMessage(dialogMsg).show();
            mScanLoadingDialog.setTag(requestCode);
            mHandleBroadcast = new HandleBroadcast(this, this);
            mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
            mSoundId = mSoundPool.load("/etc/Scan_new.ogg", 1);
        }
    }


    @Override
    public void updateText(String msg) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - mLastScanTime > 1000) {
            mLastScanTime = nowTime;
        } else {
            //如果时间间隔小于1秒不处理本次扫描
            return;
        }
        mSoundPool.play(mSoundId, 1, 1, 0, 0, 1);
        onScanSucesess(mScanLoadingDialog.getTag(),msg);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //扫描结果回调
        if (resultCode == CaptureActivity.RESULT_CODE_QR_SCAN) {
            onScanSucesess(requestCode ,data.getStringExtra(CaptureActivity.INTENT_EXTRA_KEY_QR_SCAN));
        }
    }


    @Override
    protected void onDestroy() {
        if(mHandleBroadcast != null){
            mHandleBroadcast.overBroadcastReceiver(this);
        }
        super.onDestroy();
    }

    protected void setTitleText(String titleText){
        tv_title_assets.setText(titleText);
    }

    protected void setTitleRightIcon(boolean isShow , int imgResource){
        if(isShow){
            iv_title_right.setVisibility(View.VISIBLE);
        }else{
            iv_title_right.setVisibility(View.GONE);
        }
        iv_title_right.setImageResource(imgResource);
    }

    public ImageView getIv_title_right() {
        return iv_title_right;
    }

    protected abstract void initData();

    protected abstract void onScanSucesess(int requestCode ,String scanMsg);

}
